{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "rough-firmware",
   "metadata": {},
   "source": [
    "# Peruvian Fiscal Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "preliminary-lobby",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "increasing-prague",
   "metadata": {},
   "source": [
    "The function `clean_pe_ruc()` cleans a column containing Peruvian fiscal number (RUC) strings, and standardizes them in a given format. The function `validate_pe_ruc()` validates either a single RUC strings, a column of RUC strings or a DataFrame of RUC strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "composed-description",
   "metadata": {},
   "source": [
    "RUC strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"20512333797\".\n",
    "* `standard`: RUC strings with proper whitespace in the proper places. Note that in the case of RUC, the compact format is the same as the standard one.\n",
    "* `dni`: return the DNI (CUI) part of the number for natural persons. Note if the RUC is not for natural persons, return NaN.\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_pe_ruc()` and `validate_pe_ruc()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "informed-restriction",
   "metadata": {},
   "source": [
    "### An example dataset containing RUC strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "broad-lambda",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"ruc\": [\n",
    "            \"20512333797\",\n",
    "            \"20512333798\",\n",
    "            '7542011030',\n",
    "            '7552A10004',\n",
    "            '8019010008',\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "noticed-equilibrium",
   "metadata": {},
   "source": [
    "## 1. Default `clean_pe_ruc`\n",
    "\n",
    "By default, `clean_pe_ruc` will clean ruc strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "korean-andrews",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_pe_ruc\n",
    "clean_pe_ruc(df, column = \"ruc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exposed-property",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eastern-canal",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dressed-burden",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tracked-edition",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_pe_ruc(df, column = \"ruc\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "prospective-concentrate",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "constitutional-myrtle",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_pe_ruc(df, column = \"ruc\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "suitable-auction",
   "metadata": {},
   "source": [
    "### `dni`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "paperback-helen",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_pe_ruc(df, column = \"ruc\", output_format=\"dni\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beginning-animation",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned RUC strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "intellectual-final",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_pe_ruc(df, column=\"ruc\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "forward-retreat",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "middle-substance",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "daily-deposit",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_pe_ruc(df, \"ruc\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "equivalent-block",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "moving-train",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_pe_ruc(df, \"ruc\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "headed-provider",
   "metadata": {},
   "source": [
    "## 4. `validate_pe_ruc()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "plain-retirement",
   "metadata": {},
   "source": [
    "`validate_pe_ruc()` returns `True` when the input is a valid RUC. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_pe_ruc()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_pe_ruc()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_pe_ruc()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "going-serve",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_pe_ruc\n",
    "print(validate_pe_ruc('20512333797'))\n",
    "print(validate_pe_ruc('20512333798'))\n",
    "print(validate_pe_ruc('7542011030'))\n",
    "print(validate_pe_ruc('7552A10004'))\n",
    "print(validate_pe_ruc('8019010008'))\n",
    "print(validate_pe_ruc(\"hello\"))\n",
    "print(validate_pe_ruc(np.nan))\n",
    "print(validate_pe_ruc(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "historic-aruba",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "announced-keeping",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_pe_ruc(df[\"ruc\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "simple-workshop",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "upset-escape",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_pe_ruc(df, column=\"ruc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "minimal-kentucky",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "compatible-reaction",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_pe_ruc(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "broad-battery",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
